Skip to content

video: st_mipid02: addition of ST MIPID02 CSI bridge #89764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2025

Conversation

avolmat-st
Copy link
Collaborator

The ST-MIPID02 is a CSI deserializer (up to 2 lanes) allowing to output up to 12 bit parallel interface.
It is present on the STM32MP135F discovery board and is part of the camera pipeline for this board:

GC2145 (sensor CSI) -> ST-MIPID02 -> DCMIPP (parallel interface)

This PR includes the driver and test entry for build_all. It will be added within the stm32mp135f-dk once the dcmipp PR #89407 will be merged.

This PR depends on the CCI interface PR #87935 as well as the GC2145-CSI PR #89571 in order to have the VIDEO_MIPI_ macros.

@github-actions github-actions bot added the area: Video Video subsystem label May 10, 2025
@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from f9c37d0 to 0b3ce16 Compare May 10, 2025 15:51
josuah
josuah previously approved these changes May 10, 2025
Copy link
Collaborator

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not spot blocking issues with it.
Interesting chip for Zephyr which has plenty of DVP-only boards...
Thank you for this driver!

Comment on lines 319 to 318
link_freq = ctrl.val64 * video_bits_per_pixel(desc->csi_pixelformat) /
(2 * cfg->csi.nb_lanes);

/* Enable lanes */
ret = video_write_cci_reg(&cfg->i2c, MIPID02_CLK_LANE_REG1,
(2000000000 / link_freq) << MIPID02_CLK_LANE_REG1_UI_SHIFT |
MIPID02_CLK_LANE_REG1_EN);
if (ret < 0) {
return ret;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just curious if link_freq was including some margin for vertical/horizontal blanking margins and rounding in this calculation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't here. Actually often the link_freq of the sensor is known, so in this case it is better to directly get the LINK_FREQ value from the sensor driver. When it isn't known it is calculated from the pixel_rate. This involve that the pixel_rate returned by the sensor is roughly right in order to get the correct link_freq.
Probably I could add first the LINK_FREQ control on the sensor side so that the if this one is available then we directly use it. There could be a helper for that to first try to pick the link_freq and in fallback if not available we use the pixel_rate to approximate it. Similar thing is done on Linux in fact. For the time being we don't have yet much CSI receiver in Zephyr so if we want to add stuff like that this might be a good time.

Copy link
Collaborator

@josuah josuah May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I did not know how it was all stitched together. Now I do a little better!

if we want to add stuff like that this might be a good time

Putting it in a separate PR would make this one faster to merge, but also one more PR to wait for. Both work!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, there are already several PRs which tend to get inter-dependent so I'll properly keep it like that for the moment to let those PRs get merged and will make a dedicated PR for such video-common general stuff, update required driver as well at same time.

Copy link
Collaborator

@ngphibang ngphibang May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I could add first the LINK_FREQ control on the sensor side so that the if this one is available then we directly use it. There could be a helper for that to first try to pick the link_freq and in fallback if not available we use the pixel_rate to approximate it. Similar thing is done on Linux in fact. For the time being we don't have yet much CSI receiver in Zephyr so if we want to add stuff like that this might be a good time.

I think this is a good occasion to add the link frequency control which is better than pass by the pixel rate, no ? I didn't add it when adding pixel rate as there is no user of it at that time.

Hum, there are already several PRs which tend to get inter-dependent so I'll properly keep it like that for the moment to let those PRs get merged and will make a dedicated PR for such video-common general stuff, update required driver as well at same time.

I think we can put it here in the same PR (as it is related stuff) or in a separate PR (to get merged faster), it doesn't matter. Inter-dependency is common and not a problem as long as we add the dependency tree in the PR description.

@avolmat-st
Copy link
Collaborator Author

I'm adding a new function video_get_link_frequency within the video-common in order to get the link-frequency from the source. If the link-frequency ctrl is not available (which is the case for the time being), it falls back on the pixel-rate.

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from 8dd7671 to 5489363 Compare May 12, 2025 13:52
@avolmat-st
Copy link
Collaborator Author

I've added the VIDEO_CID_LINK_FREQUENCY ctrl and a helper function. Moreover I've moved the * @name MIPI CSI2 Data-types enum in this PR so that it doesn't have dependency on the GC2145 one. I'll update the GC2145 PR in order expose the LINK_FREQUENCY ctrl instead of the PIXEL_RATE which is not really necessary now.

@avolmat-st
Copy link
Collaborator Author

New version pushed with PR #87935 commit added first in order to allow proper build of the PR.

Comment on lines 81 to 110
case VIDEO_CID_LINK_FREQUENCY:
*type = VIDEO_CTRL_TYPE_INTEGER64;
*flags |= VIDEO_CTRL_FLAG_READ_ONLY;
break;
Copy link
Collaborator

@ngphibang ngphibang May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that link frequency control is rather an array of values (integer menu), no ? This is one of the main differences and adavantages between it and the pixel rate. And we can specify the supported link frequencies in the DT like:

&camera {
    port {
        camera_ep: endpoint {
            clock-lanes = <0>;
            data-lanes = <1 2>;
            link-frequencies = /bits/ 64 <400000000 800000000>; // In Hz
        };
    };

};

It's because the link frequency is board-specific characteristic (not only determined by the sensor) and for some sensors, the master clock is provided by the upstream device.

This way, sensor driver will get the link frequency from the DT property and create the menu.

But if it seems complicated and not needed for the current usecase, we can do it later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. thanks for pointing it out. I mistaked the type it should be an interger menu. From the ST-MIPID02 point of view, it doesn't change much, it will read the value.
The ctrl has to be an integer menu and I will change that. Changes are actually more on the sensor (or source) side. To me the parsing of the DT and kind of automatic filtering of sensor capabilities / supported formats vs board allowed link-frequencies is a new helper that would have to be put in place. If you agree with that I'd like to postponed that to another PR.
I will post a new version with the fix of the ctrl type and on the GC2145 side I'll update that as well to use that as an integer menu. Then in a further PR we can introduce the helper to filter out based on link-frequencies properties from the DT.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine like that, as long as the ctrl type is consistent, other things can be added and improved later. Thanks

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from 6406a5a to 461001a Compare May 13, 2025 17:43
@avolmat-st
Copy link
Collaborator Author

New version, with LINK_FREQUENCY now being a MENU_INTEGER type. Actually this type was not available yet, only MENU (strings) were available hence I've added a new intermediate commit to add this one prior to actually adding the new control itself. Now the helper is relying on this new type, queryctrl etc to figure out the link frequency value.

I've also update my gc2145 driver to work like that. If this one (ST-MIPID02) looks right I will push the GC2145 on top of this branch then.

@avolmat-st
Copy link
Collaborator Author

I saw in the code that for the menu type it says that this is for standard or driver defined control. However I have a doubt about that since in case of ctrl which are not standard, the set_type_flag function is going to fallback to INTEGER type ctrl, hence not being possible to define custom menu ctrl I think. In order to achieve this it would actually be necessary to add a new parameter to the init functions if it is not a known ctrl.

This is not really related to this PR itself in fact.

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from 461001a to dbd1db7 Compare May 13, 2025 18:44
Copy link
Collaborator

@ngphibang ngphibang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The integer menu controls LGTM. Thanks !

@ngphibang
Copy link
Collaborator

ngphibang commented May 13, 2025

I saw in the code that for the menu type it says that this is for standard or driver defined control. However I have a doubt about that since in case of ctrl which are not standard, the set_type_flag function is going to fallback to INTEGER type ctrl, hence not being possible to define custom menu ctrl I think. In order to achieve this it would actually be necessary to add a new parameter to the init functions if it is not a known ctrl.

"Driver-defined menu" means the driver defines the menu items but the control id is known, it does not mean a private & driver-defined items menu control (I didn't see such a control, in Linux). Two examples are the test_pattern (driver-defined item) and the power line frequency (standard):

	ret = video_init_menu_ctrl(&ctrls->light_freq, dev, VIDEO_CID_POWER_LINE_FREQUENCY,
				   VIDEO_CID_POWER_LINE_FREQUENCY_50HZ, NULL);

	ret = video_init_menu_ctrl(&ctrls->test_pattern, dev, VIDEO_CID_TEST_PATTERN, 0,
				   test_pattern_menu);

@avolmat-st
Copy link
Collaborator Author

I saw in the code that for the menu type it says that this is for standard or driver defined control. However I have a doubt about that since in case of ctrl which are not standard, the set_type_flag function is going to fallback to INTEGER type ctrl, hence not being possible to define custom menu ctrl I think. In order to achieve this it would actually be necessary to add a new parameter to the init functions if it is not a known ctrl.

"Driver-defined menu" means the driver defines the menu items but the control id is known, it does not mean a private & driver-defined menu control (I didn't see such a control, in Linux). Two examples are the test_pattern (driver-defined item) and the power line frequency (standard):

	ret = video_init_menu_ctrl(&ctrls->light_freq, dev, VIDEO_CID_POWER_LINE_FREQUENCY,
				   VIDEO_CID_POWER_LINE_FREQUENCY_50HZ, NULL);

	ret = video_init_menu_ctrl(&ctrls->test_pattern, dev, VIDEO_CID_TEST_PATTERN, 0,
				   test_pattern_menu);

Understood. Right I kind of read it as driver defined control ;) OK then. Thanks for the clarification.

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from dbd1db7 to 34f00e9 Compare May 13, 2025 20:25
Copy link
Collaborator

@ngphibang ngphibang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just wait for the dependency (CCI PR) get merged first

@avolmat-st
Copy link
Collaborator Author

@ngphibang I am having kind of second thought about the link frequency helper. Should we really have that or should we simply enforce that any CSI driver MUST expose a LINK_FREQUENCY control. Currently, with the addition of the GC2145 csi, I think that is only 2 sensors being able to work in CSI. I will add the LINK_FREQUENCY ctrl to the GC2145 so only the OV5640 won't have. I guess we could add it. If we decide to enforce the requirement of LINK_FREQUENCY ctrl that helper doesn't really make sense. What do you think ?

@ngphibang
Copy link
Collaborator

@ngphibang I am having kind of second thought about the link frequency helper. Should we really have that or should we simply enforce that any CSI driver MUST expose a LINK_FREQUENCY control. Currently, with the addition of the GC2145 csi, I think that is only 2 sensors being able to work in CSI. I will add the LINK_FREQUENCY ctrl to the GC2145 so only the OV5640 won't have. I guess we could add it. If we decide to enforce the requirement of LINK_FREQUENCY ctrl that helper doesn't really make sense. What do you think ?

I think both are relevant and describe different aspects of things. Pixel rate is simpler to be used. Personally, I would not enforce anything if it's technically not wrong (e.g. if someone finds that link frequency is better they can make change btw).

@avolmat-st
Copy link
Collaborator Author

@ngphibang I am having kind of second thought about the link frequency helper. Should we really have that or should we simply enforce that any CSI driver MUST expose a LINK_FREQUENCY control. Currently, with the addition of the GC2145 csi, I think that is only 2 sensors being able to work in CSI. I will add the LINK_FREQUENCY ctrl to the GC2145 so only the OV5640 won't have. I guess we could add it. If we decide to enforce the requirement of LINK_FREQUENCY ctrl that helper doesn't really make sense. What do you think ?

I think both are relevant and describe different aspects of things. Pixel rate is simpler to be used. Personally, I would not enforce anything if it's technically not wrong (e.g. if someone finds that link frequency is better they can make change btw).

To me the real important value in case of CSI is the LINK FREQUENCY since this is that link frequency which is related to the UI necessary for configuration of a CSI PHY. From the pixel rate we can only kind of guess a link frequency "enveloppe" that would be necessary to transfer THAT amount of pixel but that's not necessarily true and the exact value. How it is transmitted and the amount of pixels are 2 different aspects I think.
However as you said, if this turns out not to be used or turns out it should be there later on we can remove it ;) So let's not spend too much time on this now.

@loicpoulain the MIPI CSI2 DT header is not part of this serie instead of the GC2145 serie (since it is also necessary for the ST-MIPI which is a dependency for the GC2145 since it introduce the LINK_FREQUENCY ctrl).

@josuah
Copy link
Collaborator

josuah commented May 18, 2025

I opened this issue to help keeping track of everywhere link frequency (and other related API changes) are discussed:

@avolmat-st
Copy link
Collaborator Author

avolmat-st commented May 24, 2025

Pushed updates based on recent review points (still open points related to adjustments related to Linux).
Diff shows changes within the CCI functions, but that's simply because I use the latest / merged commit instead. This is actually removed the next force push since I rebased on the head which already contains this commit

Next I push the same commits rebased on top of head in order to allow proper build.

Add standard data-type macros within the video.h in order
to avoid having to redefine them all in each and every CSI
based driver.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch 2 times, most recently from 4864ad7 to 784baf7 Compare May 24, 2025 20:18
@josuah josuah self-requested a review May 26, 2025 00:52
@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from 784baf7 to 45f87ef Compare May 26, 2025 18:56
@avolmat-st
Copy link
Collaborator Author

Actually this PR and the GC2145 PR (89571) both depend on each other.
The MIPID02 needs the GC2145 update in order to properly put the test case and the GC2145 PR needs the LINK_FREQ and MIPI_DT type to be defined.

Turns out I will have to move back the LINK_FREQ defintion and MIPI_DT type defintion back into the GC2145 PR in order to be able to first merge the GC2145/CSI PR.

Maybe I will first wait to have all discussion closed in this thread prior to moving the commits to the GC2145/CSI PR. Sorry for the mess ...

Copy link
Collaborator

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One might need some attention (possibly a bug?).

Everything else is more about feedback long-term, and less important details IMHO.

Very interesting part! Hope this gets some public doc soon so we can use it outside ST's DKs. :)

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch from 45f87ef to e408b75 Compare May 29, 2025 09:55
@avolmat-st
Copy link
Collaborator Author

@josuah , updated based on your remarks. This is actually linked to the GC2145 / CSI PR, (tests can't be run without having GC2145 PR merged as well and GC2145 PR needs the LINK_FREQ CID which is in THIS PR), should I actually merge both PR here ? I could simply add the 3 GC2145 commits which enable CSI within this PR which would make the merge easier ?

The other approach is that I add only the relevant commits from this PR into of the GC2145 PR so that GC2145 PR can be merged first.

@josuah
Copy link
Collaborator

josuah commented May 30, 2025

GC2145 PR needs the LINK_FREQ CID which is in THIS PR), should I actually merge both PR here ?
I could simply add the 3 GC2145 commits which enable CSI within this PR which would make the merge easier ?

Putting the hardware-independent commits (CID, MIPI data types) might work. This should be quick as most of it is reviewed here already and these additions can also be useful in downstream applications.

The other approach is that I add only the relevant commits from this PR into of the GC2145 PR so that GC2145 PR can be merged first.

If this is easy to move the commits around, then this works too! Whichever is most straightforward.

@avolmat-st avolmat-st force-pushed the video_st_mipid02_driver branch 3 times, most recently from f98b313 to b6d62ff Compare May 31, 2025 13:56
@avolmat-st
Copy link
Collaborator Author

Updated test part, this PR can thus be merged without dependencies.

@josuah josuah requested a review from ngphibang May 31, 2025 23:05
josuah
josuah previously approved these changes May 31, 2025
@@ -362,6 +362,9 @@ enum video_camera_orientation {
*/
#define VIDEO_CID_IMAGE_PROC_CLASS_BASE 0x009f0900

/** Link frequency, applicable for the CSI2 based devices. This control is read-only. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is not read-only anymore, should we update this and the commit message too ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I correct this and push it. Since those commits are also part of the GC2145/CSI PR (in order to be able to have CI OK, I also update on the GC2145/CSI PR as well).

Alain Volmat added 3 commits June 1, 2025 21:02
Add a new INTEGER_MENU type allowing to store signed 64bits
integer into a menu.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Add a ctrl VIDEO_CID_LINK_FREQ to indicate to a sink
the frequency at which a device streams data over
CSI2. Since not all source device currently provide the
LINK_FREQ control, add a helper function to retrieve
it, with a fall-back by doing an approximate via the
PIXEL_RATE control.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Addition of the support for the CSI to DVP bridge ST MIPID02.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Copy link
Collaborator

@ngphibang ngphibang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks !

Copy link

sonarqubecloud bot commented Jun 1, 2025

@kartben kartben merged commit eeab052 into zephyrproject-rtos:main Jun 2, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Video Video subsystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants